﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.events.*;
    import flash.geom.*;

    final public class ZoomTool extends Tool
    {
        var pt:Point;
        var pr:Point;
        var rect:Rectangle;

        public function ZoomTool(param1:Tuyitu)
        {
            super(param1, ToolType.Zoom, false);
            param1.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false, 0, true);
            param1.stage.addEventListener(KeyboardEvent.KEY_UP, this.KeyUp, false, 0, true);
            return;
        }// end function

        override public function Reset() : void
        {
            var _loc_1:* = ed.navigator.GetZoomPercent();
            switch(type.attitude.state)
            {
                case "plus":
                {
                    if (_loc_1 == 800)
                    {
                        type.attitude.state = "locked";
                    }
                    break;
                }
                case "minus":
                {
                    if (_loc_1 == 5)
                    {
                        type.attitude.state = "locked";
                    }
                    break;
                }
                case "locked":
                {
                    if (_loc_1 != 8 && _loc_1 != 5)
                    {
                        type.attitude.state = "plus";
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type);
            }
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            this.UpdateReferences();
            ed.cursor.freeze = true;
            ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
            ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
            this.rect = new Rectangle();
            this.pt = canvas.GetMousePosition();
            this.pr = canvas.GetRealMousePosition();
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            canvas.coating.DrawRect(Misc.CalculateRectangle(this.pr, canvas.GetRealMousePosition()));
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            this.pr = canvas.GetMousePosition();
            if (this.pr.x != this.pt.x && this.pr.y != this.pt.y)
            {
                canvas.coating.Clear();
                this.rect = Misc.CalculateRectangle(this.pt, this.pr);
                ed.navigator.SetZoom(this.rect.width > this.rect.height ? (canvas.dw / this.rect.width) : (canvas.dh / this.rect.height * 100), true);
                canvas.SetAnchor(this.pt);
                ed.navigator.RedrawMarker();
            }
            else if (event.shiftKey)
            {
                ed.navigator.ZoomMinus();
            }
            else
            {
                ed.navigator.ZoomPlus();
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        public function KeyDown(event:KeyboardEvent) : void
        {
            if (event.keyCode == 16)
            {
                type.attitude.state = "minus";
                this.Reset();
            }
            return;
        }// end function

        public function KeyUp(event:KeyboardEvent) : void
        {
            if (event.keyCode == 16)
            {
                type.attitude.state = "plus";
                this.Reset();
            }
            return;
        }// end function

        override public function CleanUp() : void
        {
            ed.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false);
            ed.stage.removeEventListener(KeyboardEvent.KEY_UP, this.KeyUp, false);
            type.attitude.state = "plus";
            return;
        }// end function

    }
}
